package com.jidong.test_springboot.shiro;


import java.util.Date;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationToken;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

public class JwtToken implements AuthenticationToken{

	private String token;
	
	public JwtToken(String token) {
		this.token=token;
	}

	
	/*获取token中的用户名用的方法**/
	@Override
	public Object getPrincipal() {
		try {
			DecodedJWT jwt=JWT.decode(token);
			String userName=jwt.getClaim("username").asString();
			return userName;
		} catch (Exception e) {
			throw new AccountException("没有登录，请重新登录");
		}
	}
	
	/*获取token中的密码用的方法****/
	@Override
	public Object getCredentials() {
		try {
			DecodedJWT jwt=JWT.decode(token);
			String password=jwt.getClaim("password").asString();
			return password;
		} catch (Exception e) {
			throw new AccountException("没有登录，请重新登录");
		}
	}
	
	
	
	public boolean isTimeout() {
		try {
			DecodedJWT jwt=JWT.decode(token);
			Date date= jwt.getExpiresAt();
			long time=date.getTime()-System.currentTimeMillis();
			if(time<0) {
				return true;
			}
			return false;
		} catch (NullPointerException e) {
			throw new AccountException("没有登录，请重新登录");
		}
	}
	
	
	
	
	
}
